﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace ColorPatterns
{
    public partial class MainPage : PhoneApplicationPage
    {
        private List<Ellipse> _circles = new List<Ellipse>();
        private List<Rectangle> _colors = new List<Rectangle>();
        private List<TextBlock> _questionMarks = new List<TextBlock>();
        private PatternGenerator _patternGenerator;
        private const int InitialSeriesNumber = 11;
        private int _currentSeriesIndex = InitialSeriesNumber;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            InitializeData();
            InitializeColorSeries();
        }

        private void InitializeData()
        {
            _circles = new List<Ellipse>
            {
                circle0, circle1, circle2, circle3, circle4, circle5, circle6, circle7, circle8,
                circle9, circle10, circle11, circle12, circle13, circle14, circle15
            };
            _colors = new List<Rectangle>
            {
                color0, color1, color2, color3, color4, color5, color6, color7, color8
            };
            for (int i = 0; i < _colors.Count; i++) _colors[i].Tag = i;
            _questionMarks = new List<TextBlock>
            {
                null, null, null, null, null, null ,null, null, null, null, null,
                questionMark11, questionMark12, questionMark13, questionMark14, questionMark15
            };
        }

        private void InitializeColorSeries()
        {
            Congratulations.Stop();
            smiley.Visibility = Visibility.Collapsed;

            _patternGenerator = new PatternGenerator();
            List<int> series = _patternGenerator.Series;

            for (int i = 0; i < InitialSeriesNumber; i++)
            {
                _circles[i].Fill = _colors[series[i]].Fill;
                _circles[i].Tag = _colors[series[i]].Tag;
            }
            for (int i = InitialSeriesNumber; i < _circles.Count; i++)
            {
                _circles[i].Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0x0D, 0x01, 0x01));
                _questionMarks[i].Visibility = Visibility.Visible;
            }
        }

        private void newPattern_Click(object sender, RoutedEventArgs e)
        {
            InitializeColorSeries();
            _currentSeriesIndex = InitialSeriesNumber;
        }

        private void color_Tap(object sender, GestureEventArgs e)
        {
            if (_currentSeriesIndex >= _circles.Count) return;

            Rectangle rectangle = (Rectangle)sender;

            AnimationFactory.CreateColorDownUpAnimation(rectangle).Begin();

            _circles[_currentSeriesIndex].Fill = rectangle.Fill;
            _circles[_currentSeriesIndex].Tag = rectangle.Tag;
            _questionMarks[_currentSeriesIndex].Visibility = Visibility.Collapsed;

            if (++_currentSeriesIndex == _circles.Count)
            {
                if (_patternGenerator.IsMatched(_circles.Select(c => (int)c.Tag).ToList()))
                {
                    smiley.Visibility = Visibility.Visible;
                    Congratulations.Begin();
                }
            }
        }

        private void circle_Tap(object sender, GestureEventArgs eventArgs)
        {
            if (smiley.Visibility == Visibility.Visible) return;
            
            Ellipse circle = (Ellipse)sender;
            if (_circles[_currentSeriesIndex - 1] == circle)
            {
                Storyboard storyboard = AnimationFactory.CreateColorTransitionAnimation(circle);
                storyboard.Completed += ((s, e) =>
                {
                    circle.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0x0D, 0x01, 0x01));
                    _questionMarks[--_currentSeriesIndex].Visibility = Visibility.Visible;
                    circle.Opacity = 1;
                });
                storyboard.Begin();
            }
        }
    }
}